var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});
// 登录接口---表单提交使用接口
router.post("/login", function (req, res) {
  console.log(req.body.sex)
  if (req.body.username == "xiaoli") {
    if (req.body.age == "18") {
      res.send("登录成功")
    } else {
      res.send("年龄不符")
    }
  } else {
    res.send("用户名错误")
  }
})
// 获取商品数据
router.get("/prolist", function (req, res) {
  var arr = [
    { pname: "可乐", price: 1888 },
    { pname: "雪碧", price: 88 },
    { pname: "咖啡", price: 99 },
    { pname: "娃哈哈", price: 166 },
    { pname: "怡宝", price: 2 },
    { pname: "百岁山", price: 3 }
  ]
  if (req.query.page == "1") {
    res.send(arr.slice(0, 3))
  } else if (req.query.page == "2") {
    res.send(arr.slice(3))
  } else {
    res.send(arr)
  }
})
// 搜索
router.post("/search", function (req, res) {
  var arr = [
    { pname: "可乐", price: 1888 },
    { pname: "雪碧", price: 88 },
    { pname: "咖啡", price: 99 },
    { pname: "娃哈哈", price: 166 },
    { pname: "怡宝", price: 2 },
    { pname: "百岁山", price: 3 },
    { pname: "零度可乐", price: 3 },
    { pname: "非常可乐", price: 3 },
    { pname: "印度红牛", price: 3 }
  ]
  // req.body.searchText == 可乐
  var sarr = arr.filter(function (ele, index) {
    return ele.pname.indexOf(req.body.searchText) > -1
  })
  res.send(sarr)


})

module.exports = router;
